package view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.util.Vector;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import various.Constants;
import controller.ControllerBoard;

import model.ModelPawnCK;
import model.ModelPlayerCK;
import model.ModelTileCK;


/**
 * Cette vue est la vue principal du jeux, on y integrer les differents elements tel que la grille de jeux, l'affichage des joueurs et leurs pions
 * le menu etc.
 * @author kn -cm
 *
 */
public class ViewBoard implements Runnable {

	private static final long serialVersionUID = 1L;
	public JFrame frame;
	public JPanel panprinc;
	public JPanel panstock;
	public JPanel panright;
	public JPanel panaction;
	public static ModelTileCK imagePicked;
	public static String thePlayer;
	public JPanel pannames;
	public JPanel panbtnnext;
	public JLabel currentPlayer;
	public JDialog dialog;
	public JTextArea noms;
	public JComboBox nom;
	public Vector<String> types;
	public Vector<String> namesPlayers;
	public ViewStockTop stockTop;
	public ViewSquare vst;
	public JLabel nbcartes;
	public JScrollPane jsp;
	public JPanel pangrd ;
	public int nbPlayers;
	ControllerBoard cb;

	public void run() {

		cb = new ControllerBoard(this);
		frame = new JFrame();
		namesPlayers = new Vector<String>();

		JMenuBar menu = new JMenuBar();
		JMenu menu1 = new JMenu("Jeu");
		JMenuItem menuit1 = new JMenuItem("Nouveau Jeu");
		menuit1.setActionCommand("Nouveau");
		menuit1.addActionListener(cb);
		JMenuItem menuit2 = new JMenuItem("Connexion");
		JMenuItem menuit3 = new JMenuItem("Fermer");
		JSeparator sep = new JSeparator();
		JMenuItem menuit5 = new JMenuItem("Quitter");
		menu1.add(menuit1);
		menu1.add(menuit2);
		menu1.add(menuit3);
		menu1.add(sep);
		menu1.add(menuit5);

		menu.add(menu1);


		panprinc = new JPanel();
		panprinc.setLayout(new BorderLayout());

		vst = new ViewSquare(Constants.vectorSide*Constants.vectorSide);
		vst.setViewBoard(this);
		jsp = new JScrollPane(vst);

		jsp.getHorizontalScrollBar().setValue(2050);	
		jsp.getVerticalScrollBar().setValue(2450);
		jsp.getHorizontalScrollBar().setValue(2050);	
		jsp.setVisible(true);

		/* Partie Droite */
		panright = new JPanel();
		panright.setLayout((LayoutManager) new BoxLayout(panright, BoxLayout.Y_AXIS));

		// Panel de la pioche
		panstock = new JPanel();

		// Panel contient l'image de la pioche.
		stockTop = new ViewStockTop(this);	
		stockTop.setVisible(false);

		panaction = new JPanel();
		panaction.setLayout(new BoxLayout(panaction, BoxLayout.Y_AXIS));

		nbcartes = new JLabel ();
		panbtnnext = new JPanel();
		currentPlayer = new JLabel();
		JButton btnsuiv = new JButton("=>");
		panbtnnext.add(btnsuiv);
		btnsuiv.setActionCommand("nextPlayer");
		btnsuiv.addActionListener(cb);

		panaction.add(nbcartes);
		panaction.add(Box.createVerticalStrut(50));
		panaction.add(panbtnnext);
		panaction.add(currentPlayer);
		panstock.add(stockTop);
		panstock.add(panaction);
		panaction.setVisible(false);

		panright.add(panstock);
		panright.add(new JSeparator());

		panprinc.add("Center", jsp);
		panprinc.add("East", panright);

		frame.add("North", menu);
		frame.add(panprinc);
		frame.pack();
		frame.setVisible(true);
		JFrame.setDefaultLookAndFeelDecorated(true);
		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
	}

	/**
	 * Cette fenetre nous permet d'ajouter les joueurs et creer ensuite une partie.
	 */
	public void createDialog(){

		dialog = new JDialog(this.frame);

		dialog.setTitle("Status du jeu");

		JPanel panarea = new JPanel();
		panarea.setLayout(new BorderLayout());

		JLabel lj = new JLabel("Liste des joueurs");
		noms = new JTextArea();
		noms.setEditable(false);
		noms.setPreferredSize(new Dimension(500,150));

		panarea.add("North", lj);
		panarea.add(noms);

		JPanel panaddj = new JPanel();
		panaddj.setLayout(new BorderLayout());

		JLabel addj = new JLabel("Add Player");
		JButton btnadd = new JButton("add");
		btnadd.addActionListener(cb);
		types = new Vector<String>();
		nom = new JComboBox(types);
		nom.addItem("");
		nom.setEditable(true);
		
		JPanel panbtngo = new JPanel();
		JButton btngo = new JButton("Run");
		btngo.addActionListener(cb);
		panbtngo.add(btngo);

		panaddj.add("North", addj);
		panaddj.add(nom);
		panaddj.add("East", btnadd);
		panaddj.add("South", panbtngo);

		dialog.add("North", panarea);
		dialog.add(panaddj);
		dialog.setVisible(true);
		dialog.pack();

	}

	/**
	 * Cette vue contient tous les elements relatif au joueurs, tel que ses pions, son nom, son score.
	 * @author PapouM
	 *
	 */
	public static class ViewStockTop extends JPanel {		
		/**
		 * 
		 */
		public Graphics g;
		public ViewBoard vb;
		public static Image img;

		private static final long serialVersionUID = 1L;
		public ViewStockTop(ViewBoard vb) {
			this.vb=vb;
			g = this.getGraphics();
			super.setPreferredSize(new Dimension(Constants.sizeSquare,Constants.sizeSquare));
		}

		protected void paintComponent(Graphics g){
			super.paintComponent(g);

			if(imagePicked != null)
				g.drawImage(img, 0, 0, Constants.sizeSquare, Constants.sizeSquare, null);

			this.repaint();

		}
	}

	public static class ViewPanNames extends JPanel {		
		/**
		 * 
		 */
		public Graphics g;
		public ViewBoard vb;
		public static Image img;
		public int cpt;
		public String namesPlayers;
		public ModelPlayerCK mp;

		private static final long serialVersionUID = 1L;
		public ViewPanNames(ViewBoard vb, ModelPlayerCK mp) {
			this.mp=mp;
			//	this.cpt=cpt;
			this.vb=vb;
			g = this.getGraphics();
			super.setPreferredSize(new Dimension(250,100));
		}

		protected void paintComponent(Graphics g){
			super.paintComponent(g);
			//Horizontal
			g.drawLine(0, 0, 250, 0);
			g.drawLine(0, 20, 250, 20);
			g.drawLine(0, Constants.sizeSquare, 300, Constants.sizeSquare);
			ModelPawnCK pwck;
			String link = "";
			int playerPawns = 0;
			//pour chaque player afficher ses pionsZ

			playerPawns = this.mp.getPawns().size();
			for(int j = 0; j < playerPawns; j++)
			{
				pwck = (ModelPawnCK) this.mp.getPawns().get(j);
				// le player a une liste de pions afficher chaque lien (car risque d'extension)
				link = pwck.getUrl();
				g.drawImage(Toolkit.getDefaultToolkit().getImage(link), (j*20)+20,30, 20,20, null);
				this.setBackground(Color.WHITE);
			}
			// On affiche le nom du joueur			
			g.drawString(this.mp.getName(), 25, 15);

						this.repaint();

		}
	}
}
